package com.zy.security;

import com.zy.entity.ZyMenu;
import com.zy.mapper.ZyMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserMenuUtils {

    @Autowired
    private ZyMenuMapper zyMenuMapper;

    /**
     * 根据权限获取当前用户所拥有的菜单项
     * username 当前登录用户的username
     * @param username
     * @return
     */
    public List<ZyMenu> getUserMenu(String username) {

        //获取父菜单
        List<ZyMenu> parentMenu = zyMenuMapper.getParentMenu(username);

        for (ZyMenu menu : parentMenu) {
            //获取父菜单id
            Integer parentMenuId = menu.getMenuId();

            //根据父菜单id和用户权限查询对应的子菜单
            List<ZyMenu> childrenMenu = zyMenuMapper.getChildrenMenuByParentMenuId(parentMenuId, username);

            //将子菜单加入父菜单
            menu.setChildren(childrenMenu);
        }

        return parentMenu;
    }

}
